package org.flckphoto;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.media.ThumbnailUtils;


/**
 * sub module of the Gallery module, that handles page information
 */
public class Page {
	private static final String TAG = "Page";
	private String[] pictures;
	private Bitmap[] loadedPictures;
	public Page(String[] pics) {
		pictures = pics;
	}
	
	/**
	 * Loads all pictures currently sett on this page
	 */
	public void loadPictures() {
		if(loadedPictures == null) {
			loadedPictures = new Bitmap[pictures.length];
			for(int i = 0; i < loadedPictures.length; i++){
				try {
					Bitmap bm = ThumbnailUtils.extractThumbnail(
										Util.optimizeImage(pictures[i]),
										Util.dpToPixels(125),
										Util.dpToPixels(125));
					loadedPictures[i] = bm;
				}
				catch(OutOfMemoryError e) {
					Log.e(TAG, "Images are too big, out of memory");
				}
			}
		}
	}
	
	/**
	 * Unloads all pictures loaded on this page and frees the memory
	 */
	public void unloadPictures() {
		if(loadedPictures != null)
			for(int i = 0; i < loadedPictures.length; i++) {
				if(loadedPictures[i] != null)
					loadedPictures[i].recycle();
			}
		loadedPictures = null;
	}
	
	/**
	 * Obtains the picture at the index given (0-5)
	 * @param index the index of the picture you want
	 * @return the Bitmap object that is at index
	 */
	public Bitmap getPicture(int index) {
		return loadedPictures[index];
	}
	
	/**
	 * Gets all of the paths for all picutres on this page
	 * @return string array with all paths on this page
	 */
	public String[] getPaths() {
		return pictures;
	}
	
	/**
	 * Counts the number of pictures on this page
	 * @return number of pictures on the page
	 */
	public int numPics() {
		return loadedPictures.length;
	}
}
